package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.FirmsListEvent;
	import net.optix.ip.events.GetInventionsListEvent;
	import net.optix.ip.events.GetInventorsListEvent;
	import net.optix.ip.events.PractitionersListEvent;
	import net.optix.ip.model.ModelLocator;
	

	public class CheckLoginCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();


		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			var delegate:CheckLoginDelegate	=	new CheckLoginDelegate(this);
			delegate.checkLogin();
		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{
			var eve0:GetInventionsListEvent = new GetInventionsListEvent();
			try
			{
				model.currentUser	=	event.result;
				model.role			=	model.currentUser.Role;
				CairngormEventDispatcher.getInstance().dispatchEvent(eve0);
			}
			catch(error:Error)
			{
				model.role			=	0;
			}
			if(model.role > 0)
			{
				if(int(model.currentUser.IPManager) == 1 )
				{
					model.viewSelected	=	7;
					var firmsListEvent:FirmsListEvent	=	new FirmsListEvent();
					CairngormEventDispatcher.getInstance().dispatchEvent(firmsListEvent);
				}
				else
				{
					var event1:GetInventionsListEvent	=	new GetInventionsListEvent();
					CairngormEventDispatcher.getInstance().dispatchEvent(event1);
				}
			}

		}
		
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			trace(event.fault);
			model.role			=	0;
			model.result		=	"Program error, please try again";
		}

	}
}